﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Farinfo.Infrastructure;
using Newtonsoft.Json;
using OneWorks.Web.Common.ActionResults;

namespace OneWorks.Web.Common
{
    public class BaseController : Controller
    {
        protected internal CustomJsonResult<T> CustomJson<T>(T data, Formatting format = Formatting.None, JsonSerializerSettings settings = null)
        {
            return new CustomJsonResult<T>(data, format, settings);
        }

        protected internal ComboResult<T> Combo<T>(IEnumerable<T> data, bool isDefault = false, string defaultText = null, string defaultValue = null)
        {
            if (defaultText != null || defaultValue != null || !isDefault)
                return new ComboResult<T>(data, isDefault, defaultText, defaultValue);
            defaultText = "==请选择==";
            defaultValue = "";
            return new ComboResult<T>(data, true, defaultText, defaultValue);
        }

        protected internal ComboResult<T> Combo<T>(IEnumerable<T> data, ComboMap<T> comboMap, bool isDefault = false, string defaultText = null, string defaultValue = null)
        {
            if (defaultText != null || defaultValue != null || !isDefault)
                return new ComboResult<T>(data, comboMap, isDefault, defaultText, defaultValue);
            defaultText = "==请选择==";
            defaultValue = "";
            return new ComboResult<T>(data, comboMap, true, defaultText, defaultValue);
        }

        protected internal AjaxResult<T> Ajax<T>(RunResult<T> runResult)
        {
            return new AjaxResult<T>(runResult);
        }

        protected internal TreeResult<T> Tree<T>(IEnumerable<T> childs, object root = null, bool isCollapse = false, bool isShowRoot = false)
        {
            return new TreeResult<T>(childs, root, isCollapse, isShowRoot);
        }


        protected internal TreeResult<T> Tree<T>(IEnumerable<T> childs, TreeMap<T> treeMap, object root = null, bool isCollapse = false, bool isShowRoot = false)
        {
            return new TreeResult<T>(childs, treeMap, root, isCollapse, isShowRoot);
        }

        protected internal DataGridResult<T> DataGrid<T>(RunResult<PagedResult<T>> runResult)
        {
            return new DataGridResult<T>(runResult.Result);
        }

        protected internal DataGridResult<T> DataGrid<T>(IEnumerable<T> runResult)
        {
            return new DataGridResult<T>(runResult);
        }

        protected internal DataGridResult<T> DataGrid<T>(RunResult<IEnumerable<T>> runResult)
        {
            return new DataGridResult<T>(new PagedResult<T>
            {
                Data = runResult.Result.ToList(),
                TotalRecords = runResult.Result.Count()
            });
        }

        protected internal TreeGrid<T> TreeGrid<T>(IEnumerable<T> childs, TreeMap<T> treeMap, bool isCollapse = false)
        {
            return new TreeGrid<T>(childs, treeMap, isCollapse);
        }
    }
}
